package com.infore.qualityControl.outsideInterface.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.infore.qualityControl.bo.QualityParamOutSelectBo;
import com.infore.qualityControl.constant.Constants;
import com.infore.qualityControl.constant.ResultConstants;
import com.infore.qualityControl.entity.EquipmentParameter;
import com.infore.qualityControl.enums.ResultStatus;
import com.infore.qualityControl.outsideInterface.service.IDataAuditOutsideService;
import com.infore.qualityControl.po.SpanDrift;
import com.infore.qualityControl.po.ZeroDrift;
import com.infore.qualityControl.repository.IEquipmentParameterRepository;
import com.infore.qualityControl.repository.ISpanDriftRepository;
import com.infore.qualityControl.repository.IZeroDriftRepository;
import com.infore.qualityControl.select.util.PageObject;
import com.infore.qualityControl.util.ArrayUtils;
import com.infore.qualityControl.util.DateUtil;
import com.infore.qualityControl.util.PageUtil;
import com.infore.qualityControl.util.ResultEntity;

@Service
public class DataAuditOutsideServiceImpl implements IDataAuditOutsideService {

    @Autowired
    IZeroDriftRepository IZeroDriftRepository;

    @Autowired
    ISpanDriftRepository ISpanDriftRepository;

    @Autowired
    IEquipmentParameterRepository IEquipmentParameterRepository;

    @Override
    public ResultEntity zeroDriftData(QualityParamOutSelectBo qualityParamOutSelectBo) {
        Date startDate = DateUtil.stringToDate(qualityParamOutSelectBo.getStartTime(), DateUtil.DATE_SMALL_YMDHMS);
        Date endDate = DateUtil.stringToDate(qualityParamOutSelectBo.getEndTime(), DateUtil.DATE_SMALL_YMDHMS);
        List<String> indexList = qualityParamOutSelectBo.getIndexCodes();
        List<String> stationCodeList = qualityParamOutSelectBo.getStationCodes();
        // 获取数据集
        List<ZeroDrift> list = IZeroDriftRepository.findByTimeBetweenAndIndexCodeInAndStationCodeInOrderByTimeDesc(startDate, endDate,
                indexList, stationCodeList);
        if (qualityParamOutSelectBo.getPageNumber() != null && qualityParamOutSelectBo.getPageSize() != null) {
            PageObject pageObject = new PageObject();
            pageObject.setPageNumber(qualityParamOutSelectBo.getPageNumber());
            pageObject.setPageSize(qualityParamOutSelectBo.getPageSize());
            return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setPage(
                    PageUtil.pageConvert(IZeroDriftRepository.findByTimeBetweenAndIndexCodeInAndStationCodeInOrderByTimeDesc(startDate,
                            endDate, indexList, stationCodeList, pageObject.getPageRequest())));
        }
        return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(list);
    }

    @Override
    public ResultEntity spanDriftData(QualityParamOutSelectBo qualityParamSelectBo) {
        Date startDate = DateUtil.stringToDate(qualityParamSelectBo.getStartTime(), DateUtil.DATE_SMALL_YMDHMS);
        Date endDate = DateUtil.stringToDate(qualityParamSelectBo.getEndTime(), DateUtil.DATE_SMALL_YMDHMS);
        List<String> indexList = qualityParamSelectBo.getIndexCodes();
        List<String> stationCodeList = qualityParamSelectBo.getStationCodes();
        List<SpanDrift> list = ISpanDriftRepository.findByTimeBetweenAndIndexCodeInAndStationCodeInOrderByTimeDesc(startDate, endDate,
                indexList, stationCodeList);
        return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(list);
    }

    @Override
    public ResultEntity equipmentParameterData(String stationCode, String indexCode) {
        if (StringUtils.isBlank(stationCode)) {
            return new ResultEntity(ResultStatus.ERROR, ResultConstants.EMPTY_PARAMETER_STATION);
        }
        if (StringUtils.isBlank(indexCode)) {
            return new ResultEntity(ResultStatus.ERROR, ResultConstants.EMPTY_PARAMETER_INDEX);
        }
        List<String> stationList = ArrayUtils.stringToList(stationCode);
        List<String> indexList = ArrayUtils.stringToList(indexCode);
        List<EquipmentParameter> list = IEquipmentParameterRepository.findByStationCodeInAndIndexCodeIn(stationList, indexList);
        return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(list);
    }

}
